package com.yonyou.pmclouds.billview.rmiimpl;

import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.billview.entity.BillViewQueryVO;
import com.yonyou.pmclouds.billview.rmitf.BillViewWrapService;
import com.yonyou.pmclouds.billview.rmitf.IBillViewBuziService;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BillViewWrapServiceImpl implements BillViewWrapService{

    private Map<String, IBillViewBuziService> billViewMap = new HashMap<>();

    @Autowired
    private ProjectSimpleQuery projectSimpleQuery;

    @Override
    public void addBuziService(String billType,IBillViewBuziService billViewBuziService){
        billViewMap.put(billType,billViewBuziService);
    }

    @Override
    public PageObject<Object> queryListByType(BillViewQueryVO billViewQueryVO) throws BusinessException {
        //获得项目ID数组
        String[] permProjects = getPermProjects(billViewQueryVO);

        if(ArrayUtils.isEmpty(permProjects))
            return new PageObject<Object>(new SuperVO[0],0,billViewQueryVO.getPageStart());

        billViewQueryVO.setPkProject(permProjects);
        //根据单据类型确定查询哪种类型的单据表
        IBillViewBuziService billViewBuziService = billViewMap.get(billViewQueryVO.getBillType());

        if(billViewBuziService == null)
            throw new BusinessException("该单据类型不支持单据查看");
        //返回展示信息
        return billViewBuziService.queryBillList(billViewQueryVO);
    }

    private String[] getPermProjects(BillViewQueryVO billViewQueryVO) throws BusinessException{
        //判断用户是否选择了想要查看的项目下面的单据
        if(ArrayUtils.isNotEmpty(billViewQueryVO.getPkProject()))
            return billViewQueryVO.getPkProject();
        //若没选择，则根据租户，查询租户下的所有的项目
        ProjectVO[] projectVOs = queryPermProjectByOrg(billViewQueryVO.getPkOrg());

            if(ArrayUtils.isEmpty(projectVOs))
                return new String[0];

            String[] pkProjects = new String[projectVOs.length];
            for (int i = 0 ; i<projectVOs.length;i++)  {
                pkProjects[i] = projectVOs[i].getPkProject();
            }
            //返回项目ID数组
            return pkProjects;
    }

    /**
     * 根据组织查询有权限的项目
     *
     * @param pkSrcOrg
     * @return
     */
    @Override
    public ProjectVO[] queryPermProjectByOrg(String pkSrcOrg) {

        boolean isTenant = true;
        if(!RuntimeEnvironment.getTenantId().equals(pkSrcOrg))
            isTenant = false;

        ProjectVO[] projectVOs = new ProjectVO[]{};
        if(isTenant){
            projectVOs = projectSimpleQuery.queryPMCProjects(RuntimeEnvironment.getTenantId());
        }else{
            Map<String,List<ProjectVO>> projectVOMap = projectSimpleQuery.queryProjectsByOrgs(RuntimeEnvironment.getTenantId(), new String[]{pkSrcOrg});
            if(projectVOMap.get(pkSrcOrg) != null)
                projectVOs =  projectVOMap.get(pkSrcOrg).toArray(new ProjectVO[0]);
        }

        return projectVOs;
    }

    @Override
    public SuperVO queryBillDetail(String billType, String pkBill) throws BusinessException {

        IBillViewBuziService billViewBuziService = billViewMap.get(billType);

        if(billViewBuziService == null)
            throw new BusinessException("该单据类型不支持单据查看");

        return billViewBuziService.queryBillDetail(pkBill);
    }

}
